﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using EShop.Common;
using EShop.BLL;

namespace EShop.Client
{
    public partial class frmMemberManager : frmBase
    {
        public frmMemberManager()
        {
            InitializeComponent();
        }

        DataTable datasource = new DataTable();

        int iPageSize = 0;
        int iPageCount = 0;
        int iCurrentPage = 0;
        int iDataTotal = 0;

        MemberManagerBLL oMemberManagerBLL = new MemberManagerBLL();
        private void frmMemberManager_Load(object sender, EventArgs e)
        {
            iPageSize = oShopConfigInfo.PageSize;
            btnMoveFirstItem.Enabled = true;
            btnMoveLastItem.Enabled = true;
            btnMovePreviousItem.Enabled = true;
            btnMoveNextItem.Enabled = true;
            lblCountItem.Enabled = true;
            txtPositionItem.Enabled = true;
            btnAddNewItem.Enabled = true;
            //GetDatasource();
            cbbType.SelectedIndex = 0;
        }

        private void GetDatasource()
        {
            datasource = oMemberManagerBLL.GetMember(null, cbbType.SelectedIndex);
        }
        private void GetDatasource(string strMemberID)
        {
            datasource = oMemberManagerBLL.GetMember(strMemberID, cbbType.SelectedIndex);
        }

        private void Init()
        {
            GetDatasource();
            iDataTotal = datasource.Rows.Count;
            iPageCount = iDataTotal / iPageSize;
            if (iDataTotal % iPageSize != 0) iPageCount += 1;
            lblCountItem.Text = "/" + iPageCount.ToString();
            BindData();
        }
        private void Init(string strMemberID)
        {
            GetDatasource(strMemberID);
            iDataTotal = datasource.Rows.Count;
            iPageCount = iDataTotal / iPageSize;
            if (iDataTotal % iPageSize != 0) iPageCount += 1;
            lblCountItem.Text = string.Format(lblCountItem.Text, iPageCount.ToString());
            BindData();
        }
        private void BindData()
        {
            txtPositionItem.Text = (iCurrentPage + 1).ToString();
            int sPos, ePos;
            if (iCurrentPage >= iPageCount-1)
            {
                sPos = iPageSize * (iPageCount-1);
                ePos = iDataTotal;
            }
            else
            {
                sPos = iPageSize * iCurrentPage;
                ePos = sPos + iPageSize;
            }
            dgvMemberList.Rows.Clear();
            int index = -1;
            if (iDataTotal <= 0) return;
            for (int i = sPos; i < ePos; i++)
            {
                index = dgvMemberList.Rows.Add();
                dgvMemberList.Rows[index].Cells["MemberID"].Value = datasource.Rows[i]["MemberID"];
                dgvMemberList.Rows[index].Cells["MemberName"].Value = datasource.Rows[i]["MemberName"];
                dgvMemberList.Rows[index].Cells["MemberSex"].Value = Convert.ToByte(datasource.Rows[i]["MemberSex"]) == 0 ? "男" : "女";
                dgvMemberList.Rows[index].Cells["Brithday"].Value = Convert.ToDateTime(datasource.Rows[i]["Brithday"]).ToString("MM-dd");
                dgvMemberList.Rows[index].Cells["IsGregorianCalendar"].Value = Convert.ToByte(datasource.Rows[i]["IsGregorianCalendar"]) == 0 ? "农历" : "公历";
                dgvMemberList.Rows[index].Cells["LevelName"].Value = datasource.Rows[i]["LevelName"];
                dgvMemberList.Rows[index].Cells["Type"].Value = oMemberManagerBLL.GetStatus(Convert.ToInt32(datasource.Rows[i]["Type"]));
            }
        }

        private void cbbType_SelectedIndexChanged(object sender, EventArgs e)
        {
            Init();
        }

        private void btnMoveFirstItem_Click(object sender, EventArgs e)
        {
            if (iCurrentPage == 0) return;
            iCurrentPage = 0;
            BindData();
        }

        private void btnMovePreviousItem_Click(object sender, EventArgs e)
        {
            if (iCurrentPage == 0) return;
            iCurrentPage -= 1;
            BindData();
        }

        private void btnMoveNextItem_Click(object sender, EventArgs e)
        {
            if (iCurrentPage >= (iPageCount - 1)) return;
            iCurrentPage += 1;
            BindData();
        }

        private void btnMoveLastItem_Click(object sender, EventArgs e)
        {
            if (iCurrentPage == (iPageCount - 1)) return;
            iCurrentPage = iPageCount - 1;
            BindData();
        }

        private void txtPositionItem_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                int i = int.Parse(txtPositionItem.Text);
                iCurrentPage = i - 1;
                BindData();
            }
        }

        private void txtPositionItem_TextChanged(object sender, EventArgs e)
        {
            try
            {
                int i = int.Parse(txtPositionItem.Text);
                if (i > iPageCount || i < 1)
                {
                    txtPositionItem.Text = (iCurrentPage + 1).ToString();
                }
            }
            catch
            {
                txtPositionItem.Text = (iCurrentPage + 1).ToString();
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtMemberID.Text)) return;
            GetDatasource(SystemLibrary.SQLQuoteName(txtMemberID.Text.Trim()));
            Init(txtMemberID.Text);
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            this.txtMemberID.Clear();
            GetDatasource();
            Init();
        }

        private void txtMemberID_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                btnSearch_Click(sender, new EventArgs());
            }
        }

        private void btnAddNewItem_Click(object sender, EventArgs e)
        {
            frmMemberAppend oMemberAppend = new frmMemberAppend();
            oMemberAppend.Owner = this;
            if (oMemberAppend.ShowDialog() == DialogResult.OK)
            {
                Init();
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            string strMemberID = string.Empty;
            try
            {
                strMemberID = dgvMemberList.Rows[dgvMemberList.SelectedRows[0].Index].Cells["MemberID"].Value.ToString();
            }
            catch
            { }
            if (string.IsNullOrEmpty(strMemberID)) return;
            frmMemberAppend oMemberAppend = new frmMemberAppend(strMemberID);
            oMemberAppend.Owner = this;
            if (oMemberAppend.ShowDialog() == DialogResult.OK)
            {
                Init();
            }
        }

        private void 查看会员历史消费ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string strMemberID = string.Empty;
            try
            {
                strMemberID = dgvMemberList.Rows[dgvMemberList.SelectedRows[0].Index].Cells["MemberID"].Value.ToString();
            }
            catch
            { }
            if (string.IsNullOrEmpty(strMemberID)) return;
            frmMemberConsumeHistory oMemberConsumeHistory = new frmMemberConsumeHistory(strMemberID);
            oMemberConsumeHistory.ShowDialog();
        }

        private void 查看修改会员信息ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            btnEdit_Click(sender, e);
        }

        private void 添加新会员ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            btnAddNewItem_Click(sender, e);
        }
    }
}
